package ma.projet.managedbean;

import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;

import javax.faces.context.FacesContext;

import ma.projet.beans.RendezVous;
import ma.projet.service.RendezVousService;

import org.primefaces.model.DefaultScheduleEvent;
import org.primefaces.model.DefaultScheduleModel;
import org.primefaces.model.ScheduleModel;

public class RendezVousSchedule {
	private RendezVousService rdvs;
	private ScheduleModel eventModel;
	private int id;
	public RendezVousSchedule() {
		// TODO Auto-generated constructor stub
		
		
	}
	public void remplir(int ide){
		init();
		id=ide;
		
		eventModel = new DefaultScheduleModel();
		rdvs = new RendezVousService();
		RendezVous rdv = new RendezVous();
		Iterator<RendezVous> it = rdvs.load().iterator();
		DefaultScheduleEvent def=null;
		while (it.hasNext()) {
			rdv = it.next();
			// if(rdv.medecin.getId()==Integer.parseInt(id))

			if (rdv.medecin.getId() == id) {
				def = new DefaultScheduleEvent(rdv.patient.toString(), madate(
						rdv.getDater(), rdv.getHeure()), madate(rdv.getDater(),
						rdv.getHeure()), "public-event");
				def.setData(rdv.patient);
				
			}

			else {
				def=new DefaultScheduleEvent(rdv.getPatient()
						.toString(), madate(rdv.getDater(), rdv.getHeure()),
						madate(rdv.getDater(), rdv.getHeure()),rdv.patient);
				
			}
			eventModel.addEvent(def);
		}
//
//		
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
	public RendezVousService getRdvs() {
		return rdvs;
	}

	public void setRdvs(RendezVousService rdvs) {
		this.rdvs = rdvs;
	}

	public ScheduleModel getEventModel() {
		return eventModel;
	}

	public void setEventModel(ScheduleModel eventModel) {
		this.eventModel = eventModel;
	}

	private Date madate(Date d, int h) {
		Calendar t = Calendar.getInstance();
		t.setTime(d);
		// if(h)
		t.set(Calendar.AM_PM, Calendar.AM);
		t.set(Calendar.DATE, t.get(Calendar.DATE));
		t.set(Calendar.HOUR, h + 1);

		return t.getTime();
	}
		
	public void init()
	{
		FacesContext.getCurrentInstance().getViewRoot().getViewMap().remove("RendezVousSchedule");
		
	}

}
